$(function() {

	$("#log_out").click(function() {
		$.ajax({
			type: "GET",
			url: "http://127.0.0.1:8080/user/logout",
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					window.location.href = './login.html';
				}
			}
		});
	})

	$("#basic-datatables").on("click", ".order-info", function() {
		var data = table.row($(this).parents('tr')).data();
		$.ajax({
			type: "GET",
			url: "http://127.0.0.1:8080/order/getOrder",
			dataType: "json",
			data: {
				"id": data.id
			},
			success: function(data) {
				var code = data.code;
				var obj = data.data;
				if (code == 'success') {
					var html = "";
					obj.forEach(function(orderItems) {
						var goods = orderItems.goods;
						var specification = goods.specification;
						var spStr = "其他";
						if (specification == 1) {
							spStr = "全网通版 6GB+64GB";
						} else if (specification == 2) {
							spStr = "全网通版 6GB+128GB";
						}
						var nature = goods.nature;
						var naStr = "其他";
						if (nature == 1) {
							naStr = "黑亮色";
						} else if (nature == 2) {
							naStr = "魅力蓝";
						} else if (nature == 3) {
							naStr = "活力红";
						}
						html += '<hr/><div class="form-group form-inline"><label class="col-md-2">商品名称:' +
							'</label><label class="col-md-3">' + goods.goodsName + '</label>' +
							'<label class="col-md-2"></label><label class="col-md-2">商品规格:' +
							'</label><label>' + spStr + '</label></div>';
						html += '<div class="form-group form-inline"><label class="col-md-2">商品属性:' +
							'</label><label class="col-md-3">' + naStr + '</label>' +
							'<label class="col-md-2"></label><label class="col-md-2">商品价格:' +
							'</label><label>￥' + goods.price + '</label></div>';
					});
					$("#goodsList").html(html);
					$("#addRowModal").modal('show');
				} else {
					var content = {};
					content.title = '警告提示';
					content.message = data.msg;
					$.notify(content, {
						type: "warning",
						placement: {
							from: "top",
							align: "center"
						},
						time: 2000,
						delay: 1,
					});
				}
			}
		});
	});

	var table = $('#basic-datatables').DataTable({
		language: {
			"sLengthMenu": "显示 _MENU_ 项结果",
			"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
			"sSearch": "搜索:",
			"oPaginate": {
				"sPrevious": "上页",
				"sNext": "下页"
			}
		},
		pageLength: 10,
		pagingType: "simple",
		bFilter: false,
		bLengthChange: true,
		serverSide: true,
		ajax: function(data, callback, settings) {
			var pagesize = data.length;
			var page = (data.start) / data.length + 1;
			var content = {};
			content.title = '警告提示';
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/order/findOrderList",
				cache: false,
				data: {
					"pageNumber": page,
					"pageSize": pagesize
				},
				dataType: "json",
				success: function(data) {
					var code = data.code;
					if (code == 'success') {
						var returnData = {};
						returnData.recordsTotal = data.data.count;
						returnData.recordsFiltered = data.data.count;
						returnData.data = data.data.orderList;
						callback(returnData);
					} else {
						content.message = data.msg;
						$.notify(content, {
							type: "warning",
							placement: {
								from: "top",
								align: "center"
							},
							time: 2000,
							delay: 1,
						});
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					content.message = '获取分页数据失败';
					$.notify(content, {
						type: "warning",
						placement: {
							from: "top",
							align: "center"
						},
						time: 2000,
						delay: 1,
					});
				}
			});
		},
		columns: [{
				"data": "orderId",
				"defaultContent": "<i></i>"
			},
			{
				"data": "createDate",
				"defaultContent": "<i></i>",
				"render": function ChangeDateFormat(date) {
					if (date == null) {
						return "";
					} else {
						var dateee = new Date(date).toJSON();
						return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,
							'');
					}

				}
			},
			{
				"data": "status",
				"defaultContent": "<i></i>",
				"render": function statusMap(status) {
					if (status == 0) {
						return "新创建";
					} else if (status == 1) {
						return "已发货";
					} else if (status == 2) {
						return "已完成";
					}
				}
			},
			{
				"data": "amount",
				"defaultContent": "<i></i>"
			}, {
				"data": "",
				"defaultContent": '<div class="form-button-action"><button type="button" data-toggle="tooltip" title="" class="btn btn-link btn-primary btn-lg order-info" data-original-title="Edit Task"><i class="fa fa-edit">订单详情</i></button></div>'
			},
		]
	});

	$.ajax({
		type: "GET",
		url: "http://127.0.0.1:8080/mgrUser/getMgrSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var node = $("#user_info");
				node.empty();
				var html = data.data.userName + '<span class="user-level">管理员</span><span class="caret"></span>';
				node.html(html);
			} else {
				window.location.href = './login.html';
			}
		}
	});
})

Date.prototype.Format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" +
			o[k]).substr(("" + o[k]).length)));
	return fmt;
}
